/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

/**
 *
 * @author carlos
 */
import java.awt.event.ActionEvent;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import jpa.entities.Usuario;
import jpa.session.UsuarioFacade;
import jsf.util.JsfUtil;

@ManagedBean(name = "userManager")
@SessionScoped
public class UserManager implements Serializable {

    private Usuario user = null;
    @EJB
    private jpa.session.UsuarioFacade ejbFacade;
    private String username;
    private String password;
    private boolean admin;
    private boolean isExterno = false;

    public boolean isIsExterno() {
        return isExterno;
    }

    public void setIsExterno(boolean isExterno) {
        this.isExterno = isExterno;
    }

    public Usuario getUser() {
        if (user == null) {
            return null;

        }
        return user;
    }

    public void setFacade(UsuarioFacade facade) {
        this.ejbFacade = facade;
    }

    private UsuarioFacade getFacade() {
        return ejbFacade;
    }

    public String recuperarLogin() {
        try {
            this.user = getFacade().findUsuarioLogado(this.username);
            admin = (this.user.getTipo() == 1 ? true : false);

        } catch (Exception e) {
            JsfUtil.addErrorMessage("Usuário ou Senha incorretos");
            return "/index?faces-redirect=true";
        }
        return this.redirect();

    }

    public String login() {
        this.logout();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        if (context.getExternalContext().getRequestParameterMap().get("isExterno") != null) {
            this.isExterno = Boolean.parseBoolean(context.getExternalContext().getRequestParameterMap().get("isExterno"));
        }
        try {
            this.user = getFacade().findFilters(this.username, EncriptaSenha.encripta(this.password));
            admin = (this.user.getTipo() == 1 ? true : false);
            if (!this.isExterno) {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("itemPedidoController");
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("buyManager");
                return this.redirect();
            }else{
                return "itemPedido/ListCliente.xhtml";
            }
            
        } catch (Exception e) {

            JsfUtil.addErrorMessage("Usuário ou Senha incorretos");
        }
        return null;




    }

    public String logout() {

        if (isAuthenticated()) {
            this.user = null;
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("itemPedidoController");
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("buyManager");


        }
        return this.redirect();


    }

    public String redirect() {
        return "/index?faces-redirect=true";


    }

    public boolean isAuthenticated() {
        if (this.user != null) {
            return true;


        }
        return false;


    }

    public boolean isAdmin() {
        return admin;


    }

    public String getUsername() {
        return username;


    }

    public void setUsername(String username) {
        this.username = username;


    }

    public String getPassword() {
        return password;


    }

    public void setPassword(String password) {
        this.password = password;

    }
}
